package com.zhy.learning.demo.controller;

import com.zhy.learning.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CountDownLatch;

/**
 * @author zhy
 * @version 1.0.0
 * @ClassName DemoControll.java
 * @Description TODO
 * @createTime 2021年09月01日 21:31:00
 */
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private DemoService demoService;

    @RequestMapping("/sell")
    public void sell() {
        CountDownLatch countDownLatch = new CountDownLatch(1000);
        for (int i = 0; i < 1000; i++) {
            new Thread(() -> {
                try {
                    countDownLatch.await();
                    demoService.testTransactional();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
            countDownLatch.countDown();
        }
    }

    @RequestMapping("/sell2")
    public void sell2() {
        CountDownLatch countDownLatch = new CountDownLatch(100);
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                try {
                    countDownLatch.await();
                    demoService.sell();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
            countDownLatch.countDown();
        }
    }
}
